package com.kinggm.utils;

import com.google.common.util.concurrent.RateLimiter;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ApiLimitUtil {

    // 保存每个接口的限流器
    public static Map<String, RateLimiter> map = new ConcurrentHashMap<>();

    /**
     * @param key 接口唯一标识
     * @param count 每秒允许请求的次数
     * @return RateLimiter
     */
    public static RateLimiter getRateLimiter(String key, double count) {
        RateLimiter rateLimiter = map.get(key);
        if(rateLimiter==null){
            rateLimiter = RateLimiter.create(count);
            map.put(key,rateLimiter);
        }
        return rateLimiter;
    }


}
